<template>
	<view scroll-y="true" class="pageAll">
		<view class="tagView">
			<view class="subTagView">
				<tag @changeTag="changeTag" :tagInfo="tagInfo" :currTag="currTag" />
			</view>
		</view>
		<scroll-view scroll-y="true" class="orderList" scroll-with-animation="true" v-if="isAlready" @scrolltolower="scrollToLower ">
			<view v-for="(item, index) in list" :key="index" class="cardView" @click="showOrder(item.title, item.id)">
				<orderCard :info="item" :currTag="currTag" @cardOperating="cardOperating" />
			</view>
		</scroll-view>
		<nonePage v-if="!(list && list.length > 0)" />
		<popupRefuse @submitRufuse="submitRufuse" @cancelRufuse="cancelRufuse" :modelVisible="popupVisible" :type="currTag" />
		<confirmFliePass @cancelPass="cancelPass" @submitPass="submitPass" :fliePassVisible="fliePassVisible" />
		<view class="boxShadow"></view>
	</view>
</template>

<script>
	import tag from '../../../components/foodReport/myTag.vue'
	import orderCard from '../../../components/purchaseOrder/orderCard.vue'
	import popupRefuse from '../../../components/purchaseOrder/popupRefuse.vue'
	import confirmFliePass from '../../../components/purchaseOrder/confirmFliePass.vue'
	import nonePage from '../../../components/commonCom/nonePage.vue'
	import { HBValidate } from '../../../utils/tools.js'
	let inter = null // 轮询
	export default {
		data() {
			return {
				popupVisible: false,
				fliePassVisible: false,
				list: [],
				currTag: 0,
				currPage: 1,
				lastPage: 1,
				nowSelectedOrderID: null,
				isAlready: false,
				tagInfo: [{
						title: '待确认',
						value: 1,
						tagNum: null,
						code: 'await_decided',
					},
					{
						title: '已确认',
						value: 2,
						tagNum: null,
						code: 'decided',
					},
					{
						title: '已发货',
						value: 3,
						tagNum: null,
						code: 'shipped',
					},
					{
						title: '已收货',
						value: 4,
						tagNum: null,
						code: 'received',
					}
				],
				pageSize: 8,
			}
		},
		methods: {
			// 下滑刷新
			scrollToLower() {
				if (this.currPage < this.lastPage) {
					this.currPage++
					this.getOrderList(this.currTag, true)
				}
			},
			// 确认订单
			submitPass(value) {
				const validate = new HBValidate()
				if (validate.isFilePwd(value)) {
					this.$api.orderAffirm({
							id: this.nowSelectedOrderID,
							order_password: value
						})
						.then(res => {
							if (res.status) {
								this.fliePassVisible = false
								this.currTag = 2;
							} else {
								uni.showToast({
									title: res.msg,
									duration: 2000,
									icon: 'none'
								})
							}
						})
				}  else {
					uni.showToast({
						title: '文件确认密码格式错误',
						icon: 'none',
						duration: 1500
					})
				}
			},
			// 取消确认订单
			cancelPass() {
				this.fliePassVisible = false
			},
			// 取消拒绝订单
			cancelRufuse() {
				this.popupVisible = false
			},
			submitRufuse(value) {
				if (value.type === 1) {
					// 拒绝订单
					if (value.result) {
						this.$api.orderRefuse({
								id: this.nowSelectedOrderID,
								refuse_cause: value.result
							})
							.then(res => {
								if (res.status) {
									this.popupVisible = false
								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none',
										duration: 1500,
									})
								}
								this.getOrderList(this.currTag, false)
							})
					} else {
						uni.showToast({
							title: '内容不能为空',
							icon: 'none',
							duration: 1500
						})
					}

				} else {
					// 取消订单
					if (value.result) {
						this.$api.orderCanceled({
								id: this.nowSelectedOrderID,
								refuse_cause: value.result
							})
							.then(res => {
								if (res.status) {
									this.popupVisible = false
								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none',
										duration: 1500,
									})
								}
								this.getOrderList(this.currTag, false)
							})
					} else {
						uni.showToast({
							title: '内容不能为空',
							icon: 'none',
							duration: 1500
						})
					}
				}
			},
			toShip(id) {
				uni.navigateTo({
					url: '../editShip/editShip?id=' + id
				})
			},
			cardOperating(value) {
				if (this.currTag === 1) {
					if (value.type === 1) { // 确认
						this.fliePassVisible = true
						uni.hideTabBar({})
						this.nowSelectedOrderID = value.id
					}
					if (value.type === 2) { // 拒绝
						this.popupVisible = true
						uni.hideTabBar({})
						this.nowSelectedOrderID = value.id
					}
					if (value.type === 0) { // 删除
						this.$api.orderDelete({
								id: value.id,
							})
							.then(res => {
								if (res.status) {
									uni.showToast({
										title: res.msg,
										duration: 2000,
										icon: 'success'
									})
									this.getOrderList(this.currTag, false)
								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none',
										duration: 1500,
									})
								}
							})
					}
				}
				if (this.currTag === 2) {
					if (value.type === 1) { // 触发发货
						this.toShip(value.id)
					}
					if (value.type === 2) { // 取消
						this.popupVisible = true
						uni.hideTabBar({})
						this.nowSelectedOrderID = value.id
					}
					if (value.type === 0) { // 删除
						this.$api.orderDelete({
								id: value.id,
							})
							.then(res => {
								if (res.status) {
									uni.showToast({
										title: res.msg,
										duration: 2000,
										icon: 'success'
									})
									this.getOrderList(this.currTag, false)
								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none',
										duration: 1500,
									})
								}
							})
					}
				}
				if (this.currTag === 3 || this.currTag === 4) {
					if (value.type === 0) { // 删除
						this.$api.orderDelete({
								id: value.id,
							})
							.then(res => {
								if (res.status) {
									uni.showToast({
										title: res.msg,
										duration: 2000,
										icon: 'success'
									})
									this.getOrderList(this.currTag, false)
								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none',
										duration: 1500,
									})
								}
							})
					}
				}

			},
			showOrder(title, id) {
				uni.navigateTo({
					url: '../orderPreview/orderPreview?currTag=' + this.currTag + '&title=' + title + '&id=' + id
				})
			},
			changeTag(value) {
				this.currTag = value.value
			},
			getOrderList(value, isAdd) {
				this.list = null
				let tempCode = null
				for (let index in this.tagInfo) {
					if (this.tagInfo[index].value === value) {
						tempCode = this.tagInfo[index].code
					}
				}
				this.$api.orderList({
						status: tempCode,
						page: this.currPage,
						pagesize: this.pageSize,
					})
					.then(res => {
						if (res.status) {
							this.isAlready = true
							if (isAdd) {
								for (let index in res.data.data) {
									if (res.data.data.hasOwnProperty(index)) {
										this.list.push(res.data.data[index])
									}
								}
							} else {
								this.list = res.data.data
							}
							this.lastPage = res.data.last_page
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 1500,
							})
						}
					})
			},
			// 修改导航栏上的提示标签
			changeOrderNum(newValue) {
				return false // 不需要消除数字，所以先不执行该函数
				if (newValue === 1) {
					this.$set(this.tagInfo[0], 'tagNum', null)
				}
				if (newValue === 2) {
					this.$set(this.tagInfo[1], 'tagNum', null)
				}
				if (newValue === 3) {
					this.$set(this.tagInfo[2], 'tagNum', null)
				}
			},
			getOrderNum(newValue) {
				this.$api.orderSum({
					isNoNeedLoading: true
				})
					.then(res => {
						if (res.status) {
							this.tagInfo[0].tagNum = res.data.await_decided === 0 ? null : res.data.await_decided
							this.tagInfo[1].tagNum = res.data.decided === 0 ? null : res.data.decided
							this.tagInfo[2].tagNum = res.data.shipped === 0 ? null : res.data.shipped
							this.tagInfo[3].tagNum = res.data.received === 0 ? null : res.data.received
							this.changeOrderNum(newValue)
						}
					})
			},
			setInter() {
				inter = null
				inter = setInterval(() => {
					this.getOrderNum(this.currTag)
				}, 15000)
			},
		},
		components: {
			tag,
			orderCard,
			popupRefuse,
			confirmFliePass,
			nonePage,
		},
		onReady() {
			this.currTag = 1
		},
		onLoad() {
		},
		onUnload() {
			clearInterval(inter)
		},
		onHide() {
			clearInterval(inter)
		},
		onShow() {
			this.currPage = 1
			this.list = []
			if (this.$store.getters.getOrderCurrTag) {
				this.currTag = this.$store.getters.getOrderCurrTag
				this.$store.commit('setOrderCurrTag', null)
			}
			this.getOrderNum(this.currTag)
			this.getOrderList(this.currTag, false)
			this.setInter()
		},
		watch: {
			currTag(newValue, oldValue) {
				this.currPage = 1
				this.list = []
				this.getOrderList(newValue, false)
				this.getOrderNum(newValue)
			}
		},
		onShareAppMessage: (options) => {
			// 设置菜单中的转发按钮触发转发事件时的转发内容
			var shareObj = {
				title: "红标HONBIAO", // 默认是小程序的名称(可以写slogan等)
				path: '/pages/indexPage/authorization/authorization', // 默认是当前页面，必须是以‘/’开头的完整路径
				imageUrl: '/static/index/labelRouge.png', //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
				success: (res) => {
					// 转发成功之后的回调
					if (res.errMsg == 'shareAppMessage:ok') {}
				},
				fail: () => {
					// 转发失败之后的回调
					if (res.errMsg == 'shareAppMessage:fail cancel') {
						// 用户取消转发
					} else if (res.errMsg == 'shareAppMessage:fail') {
						// 转发失败，其中 detail message 为详细失败信息
					}
				}
			};
			return shareObj
		},
	}
</script>

<style scoped>
	.pageAll {
		box-sizing: border-box;
		background-color: #fdf5f3;
		width: 100%;
		height: 100%;
		display: inline-block;
		/* box-shadow:0rpx -100upx 150upx -110upx #000000 inset; */
		position: fixed;

	}

	.tagView {
		height: 12%;
		padding: 17upx 17upx 0 17upx;
		box-sizing: border-box;
		position:relative;
		z-index: 2;
	}

	.subTagView {
		height: 75%;
	}

	.orderList {
		height: 88%;
		overflow: auto;
		-webkit-overflow-scrolling: touch;
		box-sizing: border-box;
		position: relative;
		z-index: 1;
	}

	.cardView {
		margin: 0 17upx 17upx 17upx;
	}
	
	.boxShadow {
		box-shadow: 0upx 0upx 20upx 5upx rgba(76, 49, 40, 0.15);
		position: fixed;
		bottom: 0upx;
		width: 100%;
		z-index: 100;
		height: 1upx;
	}
</style>
